<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.a4ieve.AchievementClass" %>
<%@ page import="com.google.appengine.a4ieve.Const" %>
<%@ page import="com.google.appengine.a4ieve.Helper" %>
<%@ page import="com.google.appengine.a4ieve.entities.AchievementWrapper" %>
<%@ page import="com.google.appengine.api.datastore.*" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <link type="text/css" rel="stylesheet" href="/css/main.css"/>
</head>

<body>

<%
    String groupId = (String) request.getSession().getAttribute(Const.GROUP);
    User usr = UserServiceFactory.getUserService().getCurrentUser();
    String EMAIL_PATTERN_STRING = "[^@]+";
    Pattern pattern = Pattern.compile(EMAIL_PATTERN_STRING);
    Matcher m = pattern.matcher(usr.getEmail());
    m.find();
    Query achievementQuery = new Query(AchievementWrapper.KIND_KEY).addFilter(
            AchievementWrapper.USER_ID_PROPERTY,
            Query.FilterOperator.EQUAL,
            m.group().toLowerCase()
    ).setKeysOnly();
    DatastoreService service = DatastoreServiceFactory.getDatastoreService();
    List<Entity> entities = service.prepare(achievementQuery).asList(FetchOptions.Builder.withDefaults());
    List<Entity> newEntities = new LinkedList<Entity>();
    if (entities != null) {
        for (Entity e : entities) {
            newEntities.add(service.get(e.getKey()));
        }
        entities = newEntities;
%>
<div class="all-achievements">
    <%
        List<Entity> earned = new LinkedList<Entity>();
        List<Entity> locked = new LinkedList<Entity>();
        List<Entity> negative = new LinkedList<Entity>();
        for (Entity e : entities) {
            if (AchievementClass.EARNED.equals
                    (AchievementClass.fromId(e.getProperty(AchievementWrapper.STATUS_PROPERTY).toString()))) {
                earned.add(e);
            } else if (AchievementClass.LOCKED.equals
                    (AchievementClass.fromId(e.getProperty(AchievementWrapper.STATUS_PROPERTY).toString()))) {
                locked.add(e);
            } else if (AchievementClass.LOCKED_BAD.equals
                    (AchievementClass.fromId(e.getProperty(AchievementWrapper.STATUS_PROPERTY).toString()))) {
                negative.add(e);
            } else {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid achievemnt status");
            }
        }

        if (!earned.isEmpty()) {
    %>
    <div><h2 class="earned-achieve">Earned</h2></div>
    <div class="grid four">
        <div class="photos">
            <%
                for (Entity earnedEntity : earned) { %>
            <div class="photo"><img src=<%=Helper.getImgEncoded(earnedEntity)%>></div>
            <%
                }
            %>
        </div>
    </div>
    <%
        }

        if (!locked.isEmpty()) {
    %>
    <div><h2 class="locked-achieve">Locked</h2></div>
    <div class="grid four">
        <div class="photos">
            <%
                for (Entity lockedEntity : locked) {%>
            <div class="photo"><img src=<%=Helper.getImgEncoded(lockedEntity)%>></div>
            <%
                }
            %>
        </div>
    </div>
    <%
        }

        if (!negative.isEmpty()) {
    %>
    <div><h2 class="locked-neg-achieve">Locked negative</h2></div>
    <div class="grid four">
        <div class="photos">
            <%
                for (Entity negEntity : negative) {%>
            <div class="photo"><img src=<%=Helper.getImgEncoded(negEntity)%>></div>
            <%
                }
            %>
        </div>
    </div>
    <%
            }
        }
    %>
</div>
</body>
</html>